package com.qqt.csr.workbench.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qqt.csr.workbench.entity.ChatTimeJob;
import com.qqt.csr.workbench.mapper.ChatTimeJobMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ChatTimeJobService extends ServiceImpl<ChatTimeJobMapper, ChatTimeJob> {

    /**
     * 创建聊天定时任务
     *
     * @param skillSetConfigId
     * @param channelUserId
     * @param channelId
     * @param conversationId
     */
    public void createMonitorJob(Long skillSetConfigId, String channelUserId, String channelId, Long csId, String conversationId) {
        ChatTimeJob chatTimeJob = new ChatTimeJob();
        chatTimeJob.setCsId(csId);
        chatTimeJob.setSkillSetCfgId(skillSetConfigId);
        chatTimeJob.setConversationId(conversationId);
        chatTimeJob.setVisitorLastReplyTime(new Date());
        chatTimeJob.setVisitorId(channelUserId);
        chatTimeJob.setChannelId(channelId);
        chatTimeJob.setCsLastReplyTime(new Date());
        chatTimeJob.setVisitorWarnStatus(0);
        chatTimeJob.setCsWarnStatus(0);
        chatTimeJob.setConversationCloseFlag(0);
        chatTimeJob.setCsLastWarnTime(new Date());
        chatTimeJob.setVisitorLastWarnTime(new Date());
        this.save(chatTimeJob);
    }

    /*
     * 获取聊天定时任务列表
     */
    public List<ChatTimeJob> getList(Long lastId, int limit) {
        return this.lambdaQuery().select()
                .gt(ChatTimeJob::getId, lastId)
                .eq(ChatTimeJob::getConversationCloseFlag, 0)
                .eq(ChatTimeJob::getDeleteFlag, 0)
                .orderByAsc(ChatTimeJob::getId)
                .last("limit " + limit)
                .list();
    }

}
